const app = getApp()

import Dialog from '@vant/weapp/dialog/dialog';
import {
    showMsg
} from '../../../../utils/config.js'

import {
    reqworkOrderLog,
    reqworkOrderupdate,
    reqbyHiddenDangerId
} from '../../../../request/api.js'

Page({

    data: {
        navHeight: app.globalData.navHeight + 'px', //头部距离
        bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离
        workList: [],
        info: {},

        height: '10vh',
        divheight: "46vh",
        state:"",
        flag:true,//是否 是隐患过来
    },

    onLoad(e) {
        console.log(e);
        this.setData({
            info: JSON.parse(e.info),
            state:e.state
        })
        // console.log(JSON.parse(e));
        // let info = JSON.parse(e.info)
        // if (info.status == 6 || info.status == 7) {
        //     this.setData({
        //         height: '0',
        //         divheight: '50vh'
        //     })
        // }
        // this.getworkOrderLog(info)
    },
    async onShow() {
        if(this.data.state==1){
            // 隐患关联的工单
            this.getbyHiddenDangerId()// 隐患id获取工单信息及处理记录
            return;
        }
        if (this.data.info.status == 6 || this.data.info.status == 7) {
            this.setData({
                height: '0',
                divheight: '52vh'
            })
        }
        this.getworkOrderLog(this.data.info)
    },
    // 隐患id获取工单信息及处理记录
    async getbyHiddenDangerId(){
        const res = await reqbyHiddenDangerId(this.data.info.id)
        if (res.code !== 20000) return showMsg(res.message)
        console.log(res);
        let workList = []
        workList.unshift({
            status: 0,
        })
        if (res.data.row.workOrderLogList.length) {
            res.data.row.workOrderLogList.forEach((item, index) => {
                item.fileList2=[]
                // 处理图片
                if(item.fileList!=null){
                    item.fileList.forEach(e=>{
                        if(e.type==1 ||e.type==2){
                            item.fileList2.push({
                                url:e.file,
                            })
                        }
                    })
                }
                workList.unshift(item)
            })
        }
        if(this.data.info.status==6){
            workList.unshift({
                status: 12,
            })
        }
        this.setData({
            workList,
            info:res.data.row,
            flag:false
        })
    },
    async getworkOrderLog(info) {
        const res = await reqworkOrderLog(info.id)
        if (res.code !== 20000) return showMsg(res.message)
        console.log(res);
        // let {
        //     rows
        // } = res.data
        let workList = []
        workList.unshift({
            status: 0,
        })
        if (res.data.row.workOrderLogList.length) {
            res.data.row.workOrderLogList.forEach((item, index) => {
                item.fileList2=[]
                // 处理图片
                if(item.fileList!=null){
                    item.fileList.forEach(e=>{
                        if(e.type==1 ||e.type==2){
                            item.fileList2.push({
                                url:e.file,
                            })
                        }
                    })
                }
                workList.unshift(item)
            })
        }
        if(this.data.info.status==6){
            workList.unshift({
                status: 12,
            })
        }
        this.setData({
            workList,
            info:res.data.row
        })

    },
    // 接单 退单
    async onSave(e) {
        console.log(e.currentTarget.dataset.status);
        let result = ''
        if (e.currentTarget.dataset.status == 2) {
            result = '接单'
        }
        if (e.currentTarget.dataset.status == 3) {
            result = '退单'
        }
        let res = await reqworkOrderupdate({
            id: this.data.info.id,
            userId: app.globalData.userId,
            username: app.globalData.username,
            status: e.currentTarget.dataset.status,
            result: result,
            remark: "",
            maintenanceUserId: this.data.maintenanceUserId,
            picList: [],
            videoFile: ""
        })
        if (res.code !== 20000) return showMsg(res.message)
        showMsg("处理成功", "success")
        setTimeout(() => {
            // wx.navigateBack({
            //     delta: 1,
            // })
            this.getworkOrderLog(this.data.info)
        }, 1000)
    },

    goDetail() {
        let info = JSON.stringify(this.data.info)

        wx.navigateTo({
            url: `/packageA/pages/message-index-page/work-handle/work-handle?info=${info}`,
        })
    },
    // 去隐患详情
    onHidden(){
        let info = JSON.stringify(this.data.info)
        console.log(this.data.info);
        wx.navigateTo({
            url: `/packageA/pages/message-index-page/hidden-details/hidden-details?info=${info}&state=1`,
        })
    }
})